<template>
  <div class="page-container">
    <!-- 固定头部 -->
    <header class="header">
      <div class="header-content">
        <!-- 左侧logo -->
        <div class="logo" @click="toIndex()">
          <img
            class="image_top"
            referrerpolicy="no-referrer"
            src="./assets/img/top.png"
          />
        </div>

        <!-- 右侧按钮 -->
        <div class="header-actions">
          <el-button class="login_btn1 my_btn" @click="toAccount" v-if="name != null && name != ''">My Account</el-button>
          <el-button class="login_btn my_btn" @click="tologin" v-if="name == null || name == ''">Login</el-button>
          <el-button class="postads_btn" @click="postAds">Post&nbsp;Ads</el-button>
        </div>
      </div>
    </header>

    <!-- 可滚动内容区域 -->
    <main class="main-content">
      <div class="content">
        <!-- 这里放置你的页面主要内容 -->
        <div class="center_content">
          <div class="loginFormDiv">
              <div class="loginTitle">Login</div>
              <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
                <div class="loginTitle1">Email</div>
                <el-form-item prop="username">
                  <el-input
                    class="input"
                    v-model="loginForm.username"
                    type="text"
                    auto-complete="off"
                    placeholder="Please enter your email address"
                  >
                  </el-input>
                </el-form-item>
                <div class="loginTitle1">Password</div>
                <el-form-item prop="password">
                  <el-input
                    class="input"
                    v-model="loginForm.password"
                    type="password"
                    auto-complete="off"
                    placeholder="Please enter your password"
                    @keyup.enter.native="handleLogin"
                  >
                  </el-input>
                </el-form-item>
                <div class="loginTitle1">Captcha</div>
                <el-form-item prop="code" v-if="captchaOnOff">
                  <el-input
                    class="input"
                    v-model="loginForm.code"
                    auto-complete="off"
                    placeholder="Please enter your captcha"
                    style="width: 63%"
                    @keyup.enter.native="handleLogin"
                  >
                  </el-input>
                  <div class="login-code">
                    <img :src="codeUrl" @click="getCode" class="login-code-img"/>
                  </div>
                </el-form-item>
                <el-checkbox v-model="loginForm.rememberMe" style="margin:25px 0px 25px 0px;">rememberMe</el-checkbox>
                <el-form-item style="width:100%;">
                  <el-button
                    :loading="loading"
                    size="medium"
                    type="primary"
                    style="width:100%; height: 40px; margin-bottom: 10px; background: linear-gradient(#FF3A90 0%, #C6044B 100%); border-radius: 4px;"
                    @click.native.prevent="handleLogin"
                  >
                    <span v-if="!loading">Login</span>
                    <span v-else>Logging...</span>
                  </el-button>
    <!--              <div style="float: right;" v-if="register">-->
    <!--                <router-link class="link-type" :to="'/cmsRegister'">立即注册</router-link>-->
    <!--              </div>-->
                </el-form-item>
                <el-form-item style="width: 100%">
                  <el-button
                    size="medium"
                    type="primary"
                    style="width:100%; height: 40px; margin-bottom: 10px; color: #E81566; background: white; border-radius: 4px;"
                    @click.native.prevent="handleReg"
                  >
                    Sign Up Here
                  </el-button>
                </el-form-item>
              </el-form>
            </div>
        </div>

      </div>
    </main>

    <!-- 固定底部 -->
    <footer class="footer">
      <div class="footer1">Terms｜About&nbsp;us｜Contact&nbsp;us｜Privacy</div>
      <div class="footer1">Email:topgirlspage88@gmail.com</div>
      <hr>
      <div class="footer1">Copyright&nbsp;©&nbsp;2025&nbsp;topgirlspage.com&nbsp;All&nbsp;Rights&nbsp;Reserved</div>
    </footer>

  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        //username: "admin",
        password: "",
        //password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "Please enter your email address" }
        ],
        password: [
          { required: true, trigger: "blur", message: "Please enter your password" }
        ],
        code: [{ required: true, trigger: "change", message: "Please enter your captcha" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    toIndex() {
      this.$router.push({
        path: "/cms/main/cmsIndex"
      });
    },
    handleReg() {
      this.$router.push({ path: "/cmsRegister" });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: "/cms/main/cmsIndex" });
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
  },
}
</script>

<style scoped>
/* 重置默认边距 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.page-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

/* 头部样式 */
.header {
  height: 60px;
  background-color: white;
  color: white;
  padding: 0 20px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.image_top {
  width: 252px;
  height: 52px;
  margin: 8px 0 0 240px;
}

.header-actions {
  display: flex;
  gap: 15px;
}

.my_btn {
  height: 40px;
  background: linear-gradient( #FF3A90 0%, #C6044B 100%);
  border-radius: 6px;
  font-family: SourceHanSansCN, SourceHanSansCN;
  font-weight: 500;
  font-size: 16px;
  color: #FFFFFF;
  line-height: 24px;
  text-align: center;
  font-style: normal;
  margin: 8px 0 0 calc(100vw - 1000px);
}

.login_btn {
  width: 84px;
}

.login_btn1 {
  width: 140px;
}

.postads_btn {
  width: 105px;
  height: 40px;
  background: #2D1B0B;
  border-radius: 6px;
  font-family: SourceHanSansCN, SourceHanSansCN;
  font-weight: 500;
  font-size: 16px;
  color: #FBD798;
  line-height: 24px;
  text-align: center;
  font-style: normal;
  margin: 8px 0 0 8px;
}

/* 中间内容区域 */
.main-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background-color: #FFF6F9;
}

.content {
  /* width: 800px; */
  height: 740px;
  background: url("assets/img/bg.png");
  margin: 0 auto;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 515px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 48px;
    input {
      height: 48px;
      background: #f6f6f6;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.input {
  background-color: #DDDDDD;
}
.loginTitle {
  text-align: center;
  font-size: 28px;
  font-weight: bold;
  padding: 20px 0;
}
.loginFormDiv {
  background-color: #FFFFFF;
  width: 520px;
  /* height: 658px; */
  margin: 30px auto;
}

.flex-row {
  display: flex;
  flex-direction: row;
}

.box_1 {
  max-width: 1200px;
  height: 52px;
  /*background: url(https://lanhu-oss-2537-2.lanhuapp.com/SketchPngf5d0297da7e4a7c3d534b43a2ea85fa403a16d5a578c427828027844f7782e13) 100% no-repeat;*/
  background: url(../cms/assets/img/search_bg.png) 100% no-repeat;
  background-size: 100% 100%;
  margin: 0 auto;
  justify-content: flex-start;
}

.box_2 {
  max-width: 1200px;
  height: 14px;
  font-size: 14px;
  margin: 10px 0px;
}

.thumbnail_2 {
  padding-right: 5px;
}

.search1 {
  /* width: 200px; */
  width: calc(33vw - 40px);
  margin: auto 5px;
}

.loginTitle1 {
  margin: 15px 0;
  font-size: 16px;
}

.search2 {
  width: 120px;
  height: 40px;
  text-align: center;
  margin: auto;
  color: #E81566;
  float: right;
}

.center_content {
  width: calc(100vw - 480px);
  /* height: calc(100vh - 246px); */
  margin: 0 auto;
  overflow-y: auto;
}

.grid-content {
  margin-right: 12px;
  width: 280px;
  height: 40px;
  color: #A2044f;
}

/* 底部样式 */
.footer {
  height: 65px;
  background-color: #E81566;
  color: white;
  align-items: center;
  justify-content: center;
}

.footer1 {
  text-align: center;
  color: white;
  font-size: 14px;
  margin: 3px 0 0 0;
}
</style>
